<?php

class Pr_page extends Core {
    
    function __construct() {
        
        parent::__construct_admin();
        
        if (!$this->usr->check_rights('admin')) {
            
            show_error('Нет доступа!!!');
        }
        
        $this->load->language('page_main');
        $this->load->model('modpage');
    }
    
    function index() {
        
        $this->load->library('pagination');
        
        $vars['pagination'] = $this->pagination->create('admin/modul/page/index', $this->modpage->count_all(), $this->config->get('pagination', 'core'));
        $vars['all_pages'] = $this->modpage->get_all($this->config->get('pagination', 'core'), $this->pagination->offset())->result_array();
        
        parent::render($vars, 'admin/all', true);
    }
    
    function add() {
        
        $this->load->helper('form');
        
        $data = array();
        
        if ( $this->input->post('add', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('uri',   'URI',       'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('title', 'Заголовок', 'trim|required|min_length[2]|xss_clean');
            $this->form_validation->set_rules('text',  'Текст',     'trim|required|min_length[2]');
            
            if ( $this->form_validation->run() ) {
                
                $data['notification'] = 'Страница создана';
                $this->modpage->add(set_value('uri'), set_value('title'), set_value('text'));
            }
            else {
                
                $data['notification'] = $this->form_validation->error_string();
            }
        }
        
        parent::render($data, 'admin/add', true);
    }
    
    function edit($id) {
        
        $this->load->helper('form');
        
        if ( $this->input->post('edit', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('uri',   'URI',       'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('title', 'Заголовок', 'trim|required|min_length[2]|xss_clean');
            $this->form_validation->set_rules('text',  'Текст',     'trim|required|min_length[2]');
            
            if ( $this->form_validation->run() ) {
                
                $data['notification'] = 'Страница изменена';
                $this->modpage->update($id, set_value('uri'), set_value('title'), set_value('text'));
            }
            else {
                
                $data['notification'] = $this->form_validation->error_string();
            }
        }
        
        $data['page'] = $this->modpage->get_byid($id)->row_array();
        
        parent::render($data, 'admin/edit', true);
    }
    
    function delete($id) {
        
        $this->modpage->delete($id);
        modul_redirect('modul/page');
    }
}